import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.MyPrint;
import java.text.DecimalFormat;

import local.util.MyStringFormat;
import local.util.MyPrint;

public class SectorReport {    

    private Vector sectorRecordVector = new Vector ();
    DecimalFormat td = new DecimalFormat("0.00");
    DecimalFormat td1 = new DecimalFormat("0");
    MyStringFormat mf = new MyStringFormat();
    int totalStock = 0;
    double totalVolume = 0;
    double totalMoney = 0.0;
    String today = null;

    public static void main (String args[]) {

	SectorReport sreport = new SectorReport ();

	Vector t_head_v   = new Vector();
	Vector t_record_v = new Vector();
	Vector record_v = new Vector ();
	Hashtable t_head_h = new Hashtable ();
	String my_query_string ="SELECT * FROM sector_record ORDER BY SID, DATE";

	DataBaseQuery my_query = new DataBaseQuery ();

	if (args.length<1) {
	    sreport.printHelpMessage ();
	}

	sreport.today = args[0];
	my_query.setQueryString (my_query_string);
	my_query.lookup (my_query_string);

	t_head_v    = my_query.getTableHeadVector();
	t_record_v  = my_query.getTableRecordVector();

	sreport.setSectorRecord(t_record_v);
	sreport.printSectorReport (sreport.getSectorRecord());
    }

    private void printHelpMessage () {

	System.out.println("========================================");
	System.out.println("Generate Sector Report using db data");
	System.out.println("SectorReport [current date:2002-09-12]");
	System.out.println("========================================");
	System.exit(0);
    }

    public void setSectorRecord (Vector d_v_v) {

	Vector temp_v = new Vector();

	Enumeration enum = d_v_v.elements();
	while ( enum.hasMoreElements()) {
	    temp_v =(Vector)enum.nextElement();
	    SectorRecord s_r = new SectorRecord ();
	    s_r.setSectorRecord(temp_v);
	    sectorRecordVector.add(s_r);
	}
    }      

    public void setSectorTotal (SectorRecord record) {

	totalStock  = record.getTotalStock();
	totalVolume = record.getTotalVolume();
	totalMoney  = record.getTotalMoney();
	//System.out.println(totalStock);
	//System.out.println(totalVolume);
	//System.out.println(totalMoney);
    }      

    
    public Vector getSectorRecord ( ) {

	return sectorRecordVector;
    }      

    public void printSectorReport( Vector input_r_v) {

	int stock             = 0;
	int stock_up          = 0;
	int stock_down        = 0;
	int naza = 1504;
	double volume                 = 0;
	double volume_up              = 0;
	double volume_down            = 0;

	double money                = 0.0;
	double money_up             = 0.0;
	double money_down           = 0.0;

	double tm_up                = 1071.26;
	double tm_down              = 1071.26;
	double tm_last              = 1071.26;

	double net_money            = 0.0;
	double money_change         = 0.0;

	double sector_weight        = 0.0;

	String date                 = null;
	String sector_id            = null;
	String sector_name          = null;
	String temp                 = null;
	SectorRecord s_r = new SectorRecord ();

	NasdaqReport naz = new NasdaqReport();
	naz.setNazRecordVector("06/10/2002");

	Enumeration enum = input_r_v.elements();

	while ( enum.hasMoreElements()) { 
	    s_r = (SectorRecord)enum.nextElement();

	    date = s_r.getDate();
	    sector_name     = s_r.getSectorName();
	    sector_id       = s_r.getSectorId();

	    if (!sector_name.equals(temp)) {
		MyPrint.printLine("=",130);
		System.out.print("\t"+sector_name+"\t");
		System.out.print(sector_id);
		System.out.print("\t"+sector_name+"\t");
		System.out.println(sector_id);
		net_money = 0.0;
		money_change = 0.0;
		MyPrint.printLine("=",130);
		System.out.print(mf.leftIndent ("[DATE]",9,' '));
		System.out.print(mf.rightIndent("[Naz]",5,' '));
		System.out.print(mf.rightIndent("[MONEY-T]", 10, ' ')+" ");
		System.out.print(mf.rightIndent("[MONEY-UP]",10,' '));
		System.out.print(mf.rightIndent("[Up%]",8,' '));
		System.out.print(mf.rightIndent("[MONEY-DOWN]",10,' '));
		System.out.print(mf.rightIndent("[Down%]",8,' '));
		System.out.print(mf.rightIndent("[DAILY NET]",13,' ')+" ");
		System.out.print(mf.rightIndent("[TOTAL NET]", 10, ' '));
		System.out.println(mf.rightIndent("[Naz]",5,' '));
		MyPrint.printLine("=",130);
	    }

	    if (sector_name.equals("Total")) {
		setSectorTotal(s_r);
	    }

	    System.out.print(mf.leftIndent((date.replaceAll("00:00:00","")).replaceAll("200","0"),9,' '));
	    System.out.print(mf.rightIndent(naz.getNazChange(date),4,' '));
	    
	    stock     = s_r.getTotalStock();
	    stock_up  = s_r.getTotalStockUp();
	    stock_down= s_r.getTotalStockDown();

	    //System.out.print  (mf.rightIndent(stock, 6, ' ')+" ");
	    //System.out.print  (mf.rightIndent(total_stock_up, 4, ' ')+" ");
	    //System.out.print  (mf.rightIndent(100*stock_up/(stock), 4, ' ')+"% ");
	    //System.out.print  (mf.rightIndent(total_stock_down, 4, ' ')+" ");
	    //System.out.print  (mf.rightIndent(100*(stock_down)/(stock), 4, ' ')+"% ");

	    volume          = s_r.getTotalVolume();
	    volume_up       = s_r.getTotalVolumeUp();
	    volume_down     = s_r.getTotalVolumeDown();

	    //System.out.print(mf.rightIndent(td1.format(volume),11,' '));
	    //System.out.print(mf.rightIndent(volume_up, 10, ' ')+" "+"("+
	    //System.out.print(mf.rightIndent(td1.format(volume_up*100/volume),5,' ')+"%");
	    //System.out.print(mf.rightIndent(volume_down,10,' ')+" "+"("+
	    //System.out.print(mf.rightIndent(td1.format(volume_down*100/volume),5,' ')+"%");

	    money           = s_r.getTotalMoney();
	    money_up        = s_r.getTotalMoneyUp();
	    money_down      = s_r.getTotalMoneyDown();

	    System.out.print(mf.rightIndent(td.format(money/1000000), 10, ' ')+" ");
	    System.out.print(mf.rightIndent(td.format(money_up/1000000),10,' '));
	    System.out.print(mf.rightIndent(td.format(100*money_up/money),8,' ')+"% ");

	    System.out.print(mf.rightIndent(td.format(money_down/1000000),10,' '));
	    System.out.print(mf.rightIndent(td.format(100*money_down/money),8,' ')+"%");

	    net_money += (money_down + money_up);
	    money_change += (money_down + money_up);
	    sector_weight = (100*money/totalMoney +100*stock/totalStock+100*volume/totalVolume)/3;

	    if (!sector_name.equals("Total")) {
		System.out.print(mf.rightIndent(td.format(sector_weight),8,' ')+" ");
	    }

	    //System.out.print(mf.rightIndent(naz.getNazChange(date),5,' '));

	    //Daily net
	    System.out.print(mf.rightIndent(td.format((money_down+money_up)/1000000), 10, ' '));

	    //System.out.println(mf.rightIndent(td.format(money_change/1000000), 10, ' '));
	    //money_change = 0.0;

	    //Total net
	    naza +=naz.getNazChange(date);
	    System.out.print(mf.rightIndent(td.format(net_money/1000000),13,' ')+" ");
	    System.out.print(mf.rightIndent(naza,5,' '));

	    if(net_money > tm_up) {
		tm_up = net_money;
	    }

	    if(net_money < tm_down) {
		tm_down = net_money;
	    }
	    
	    if(net_money < tm_last && tm_up == tm_last) {
		System.out.println(mf.rightIndent(td.format((tm_up-tm_down)/1000000), 10, ' '));
		tm_down = net_money;
	    }
	    else if(net_money > tm_last && tm_down == tm_last) {
		System.out.println(mf.rightIndent(td.format((tm_down-tm_up)/1000000), 10, ' '));
		tm_up = net_money;
	    }
	    else {
		System.out.println();
	    }
	    
		// System.out.print(mf.rightIndent(td.format((tm_up)/1000000), 10, ' '));
		//System.out.print(mf.rightIndent(td.format((tm_down)/1000000), 10, ' '));
		//System.out.println(mf.rightIndent(td.format((tm_last)/1000000), 10, ' '));
	    /*
	    if(net_money < tm_up && net_money > tm_down) {
		if(tm_up > im) {
		  
		}
		if(tm_down < im) {
		    System.out.println(mf.rightIndent(td.format((tm_down-im)/1000000), 10, ' '));
		    im = tm_down;
		    tm_up = tm_down;
		}
	    }
	    else if(net_money < temp_last && tm_lst > tm_down) {
		if(tm_up > im) {
		    System.out.println(mf.rightIndent(td.format((tm_up-im)/1000000), 10, ' '));
		    im = tm_up;
		    tm_down = tm_up;
		}
		if(tm_down < im) {
		    System.out.println(mf.rightIndent(td.format((tm_down-im)/1000000), 10, ' '));
		    im = tm_down;
		    tm_up = tm_down;
		}
	    */
	  
	    /*
	    if (date.startsWith("2002-06-17") || date.startsWith("2002-07-23") || 
		date.startsWith("2002-08-22") || date.startsWith("2002-10-09") || 
		date.startsWith("2002-11-27") ||
		date.startsWith(today)) {
		System.out.println(mf.rightIndent(td.format(money_change/1000000), 10, ' '));
		money_change = 0.0;
	    }
	    else {
		System.out.println();
	    }
	    */

	    tm_last = net_money;
	    temp = sector_name;
	}


	MyPrint.printLine("=",130);
    }
}








